package com.wanyang.utils;

import com.wanyang.aliyun.configure.SmsProperties;
import org.yaml.snakeyaml.Yaml;

import java.io.*;
import java.net.URL;

public class YamlUtil {
    public static InputStream loadStream(String path) throws IOException {
        if (path.startsWith("http")) {
            URL url = new URL(path);
            return url.openStream();
        } else {
            return YamlUtil.class.getResourceAsStream(path);
        }
    }

    public static <T> T loadConf(String path, Class<T> clz) throws IOException {
        try (InputStream inputStream = loadStream(path)) {
            Yaml yaml = new Yaml();
            return yaml.loadAs(inputStream, clz);
        }
    }

    public static <T> void dumpConf(String save, T obj) throws IOException {
        Yaml yaml = new Yaml();
        yaml.dump(obj, new BufferedWriter(new FileWriter(save)));
    }


    public static void main(String[] args) throws IOException {
        SmsProperties smsProperties = YamlUtil.loadConf("/sms.yml", SmsProperties.class);
        System.out.println(smsProperties);
    }
}
